// pages/collection/collection.js
const app = getApp();
const api = require('../../utils/api');
const util = require('../../utils/util.js');
const constant = require('../../utils/constant');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //获取全局变量 导航栏的高度statusBarHeight
    statusBarHeight: app.globalData.statusBarHeight,
    // 胶囊
    jiaonang: app.globalData.jiaonang, 
    navigationHeight: app.globalData.navigationHeight,
    BASEURLWWW:constant.BASEURLWWW,
    // 分页
    pagination:{
      pageNum: 1,  //分页初始值，页号1
      pageSize: 10, //分页初始值，每页20条
      total: null
    },
    // 订单数据
    collectionListData: [],
    // 没有更多数据提示
    isLastNotice: false,
    // 是否有数据
    haveData: false,
    // 移除收藏容器
    deleteData: [],
    // 是否全选
    isChooseAll: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
      this.getCollectionListData();
  },
  // 获取收藏列表
  getCollectionListData:function(){
    let collectionListDataArr = this.data.collectionListData;
    if(collectionListDataArr.length == this.data.pagination.total){
      this.setData({
        isLastNotice: true,
      })
    }else{
      api.getCollectionList(this.data.pagination).then(res => {
        if(res.code == 0){
          if(res.data.data.total != 0){
            res.data.data.list.forEach(item => {
              item.selected = false;
            })
            collectionListDataArr.push(...res.data.data.list);
            this.setData({
              haveData: true,
              'pagination.total':  res.data.data.total,
              collectionListData: collectionListDataArr,
              isLastNotice: false,
            })
          }else{
            this.setData({
              haveData: false,
              collectionListData: [],
              'pagination.total': null,
              isLastNotice: false,
            })
          }
        }else{
          this.setData({
            haveData: false
          })
        }
      })
    }
  },
    // 上拉加载
    bindscrolltolower:function(){
      let pagination = this.data.pagination;
      pagination.pageNum++;
      this.setData({
        pagination
      })
      this.getCollectionListData();
    },
  // 返回上一级
  back:function(){
    wx.reLaunch({
      url: '/pages/personal/personal'
    })
  },
  // 跳转到商品详情
  jumpToGoods:function(e){
    wx.reLaunch({
      url: '/pages/goods/goods?goods_id=' + e.currentTarget.dataset.goodsid
    })
  },
  // 全选
  chooseAll:function(){
    let collectionListData = this.data.collectionListData;
    let deleteData = this.data.deleteData;
    collectionListData.forEach(item => {
      deleteData.push(item.collect_id);
      item.selected = true
    })
    this.setData({
      deleteData,
      collectionListData,
      isChooseAll: true
    })
  },
  // 全不选
  notChooseAll:function(){
    let collectionListData = this.data.collectionListData;
    let deleteData = this.data.deleteData;
    collectionListData.forEach(item => {
      deleteData.push(item.collect_id);
      item.selected = false
    })
    this.setData({
      deleteData: [],
      collectionListData,
      isChooseAll: false
    })
  },
  // 单选
  choose:function(e){
    let collect_id = e.currentTarget.dataset.id;
    let collectionListData = this.data.collectionListData;
    let deleteData = this.data.deleteData;
    collectionListData.forEach(item => {
      if(item.collect_id == collect_id){
        if(item.selected){
          item.selected = false;
          let index = deleteData.indexOf(item.collect_id);
          if( index > -1){
            deleteData.splice(index,1);
          }
        }else{
          item.selected = true;
          deleteData.push(item.collect_id)
        }
      }
    })
    this.setData({
      collectionListData,
      deleteData
    })
  },
  // 移除收藏
  deleteCollection:function(){
    let deleteData = this.data.deleteData;
    let that = this;
    if(deleteData.length > 0){
      let pagination = {
        pageNum: 1,  //分页初始值，页号1
        pageSize: 10, //分页初始值，每页20条
        total: null
      }
      api.deleteCollectionList(deleteData).then(res => {
        if(res.code == 0){
          that.setData({
            pagination,
            deleteData: [],
            collectionListData: []
          })
          that.getCollectionListData();
        }
      })
    }else{
      wx.showToast({
        title: '请先选择商品',
        icon: 'none'
      })
    }
    
  }
})